// ****** 从会员库中实时获取用户 ******
// * A.使用此方法必须有三个字段：
// *   1是FieldID字段，如果没有此字段你就不知道该把此效果赋给谁；
// *   2是ID对应该的中文字段;
// *   3是手机号
// * B.这两个字段必须有：id 和 name 两个属性
// * C.(1)(2)(4) 3个参数必填
// * D.UserType: 1普通会员 2企业客户 10管理员 20司机 -1全部
// ******/
function AutoAcceptUserInfo(acceptFieldId, userId, userMobile, userType) {
    $('#' + acceptFieldId).autocomplete({
        serviceUrl: '/get-similar/getSimilarUser?userType=' + userType,
        type: 'POST',
        dataType: 'json',
        paramName: 'filter',
        noCache: false,
        minChars: 2,
        onSearchStart: function (query) {
            //ajax请求前执行
            $('input[name="' + userId + '"]').val(-9999);

            if (userMobile != null) {
                $('input[name="' + userMobile + '"]').val('');
            }
        },
        transformResult: function (response) {
            return {
                suggestions: $.map(response, function (value, key) {
                    return {value: value, data: key};
                })
            };
        },
        onSelect: function (suggestion) {
            //选中值后数据处理
            $('#' + userId).val(suggestion.data);

            //自动填充用户手机号
            if (userMobile != null) {
                AutoGetUserMobile(suggestion.data, userMobile);
            }
        }
    });
}

//***实时获取开过票的公司***
function AutoAcceptInvoiceCompany(acceptFieldId, taxcode) {
    $('#' + acceptFieldId).autocomplete({
        serviceUrl: '/get-similar/getSimilarUnit?modelType=16',
        type: 'POST',
        dataType: 'json',
        paramName: 'filter',
        noCache: false,
        minChars: 2,
        onSearchStart: function (query) {
            //ajax请求前执行
            $('#' + taxcode).val('');
        },
        transformResult: function (response) {
            return {
                suggestions: $.map(response, function (value, key) {
                    return {value: value, data: key};
                })
            };
        },
        onSelect: function (suggestion) {
            //选中值后数据处理
            $('#' + taxcode).val(suggestion.data);
        }
    });
}

//***从员工库中实时获取员工***
function AutoAcceptStaffInfo(acceptFieldId, staffId) {
    $('#' + acceptFieldId).autocomplete({
        serviceUrl: '/get-similar/getSimilarStaff',
        type: 'POST',
        dataType: 'json',
        paramName: 'filter',
        noCache: false,
        minChars: 2,
        onSearchStart: function (query) {
            //ajax请求前执行
            $('input[name="' + staffId + '"]').val(-9999);
        },
        transformResult: function (response) {
            return {
                suggestions: $.map(response, function (value, key) {
                    return {value: value, data: key};
                })
            };
        },
        onSelect: function (suggestion) {
            //选中值后数据处理
            $('#' + staffId).val(suggestion.data);
        }
    });
}

//***从配件/采购库中实时获取物料***
function AutoAcceptPurchaseInfo(acceptFieldId, purchaseId) {
    $('#' + acceptFieldId).autocomplete({
        serviceUrl: '/get-similar/getSimilarPurchase',
        type: 'POST',
        dataType: 'json',
        paramName: 'filter',
        noCache: false,
        minChars: 2,
        onSearchStart: function (query) {
            //ajax请求前执行
            $('input[name="' + purchaseId + '"]').val(-9999);
        },
        transformResult: function (response) {
            return {
                suggestions: $.map(response, function (value, key) {
                    return {value: value, data: key};
                })
            };
        },
        onSelect: function (suggestion) {
            //选中值后数据处理
            $('#' + purchaseId).val(suggestion.data);
        }
    });
}

//***从司机库中实时获取司机***
function AutoAcceptDriverInfo(acceptFieldId, driverId) {
    $('#' + acceptFieldId).autocomplete({
        serviceUrl: '/get-similar/getSimilarDriver',
        type: 'POST',
        dataType: 'json',
        paramName: 'filter',
        noCache: false,
        minChars: 2,
        onSearchStart: function (query) {
            //ajax请求前执行
            $('input[name="' + driverId + '"]').val(-9999);
        },
        transformResult: function (response) {
            return {
                suggestions: $.map(response, function (value, key) {
                    return {value: value, data: key};
                })
            };
        },
        onSelect: function (suggestion) {
            //选中值后数据处理
            $('#' + driverId).val(suggestion.data);
        }
    });
}

//***从车辆库中实时获取车辆***
function AutoAcceptCarInfo(acceptFieldId, carId) {
    $('#' + acceptFieldId).autocomplete({
        serviceUrl: '/get-similar/getSimilarCar',
        type: 'POST',
        dataType: 'json',
        paramName: 'filter',
        noCache: false,
        minChars: 2,
        onSearchStart: function (query) {
            //ajax请求前执行
            $('input[name="' + carId + '"]').val(-9999);
        },
        transformResult: function (response) {
            return {
                suggestions: $.map(response, function (value, key) {
                    return {value: value, data: key};
                })
            };
        },
        onSelect: function (suggestion) {
            //选中值后数据处理
            $('#' + carId).val(suggestion.data);
        }
    });
}

//***从公司库中实时获取公司***
function AutoAcceptCompanyInfo(acceptFieldId, companyId, companyType) {
    $('#' + acceptFieldId).autocomplete({
        serviceUrl: '/get-similar/getSimilarCompany?companyType=' + companyType,
        type: 'POST',
        dataType: 'json',
        paramName: 'filter',
        noCache: false,
        minChars: 2,
        onSearchStart: function (query) {
            //ajax请求前执行
            $('input[name="' + companyId + '"]').val(-9999);
        },
        transformResult: function (response) {
            return {
                suggestions: $.map(response, function (value, key) {
                    return {value: value, data: key};
                })
            };
        },
        onSelect: function (suggestion) {
            //选中值后数据处理
            $('#' + companyId).val(suggestion.data);
        }
    });
}

//***从分类库中实时获取分类***
function AutoAcceptCatalogInfo(acceptFieldId, catalogId, modelType) {
    $('#' + acceptFieldId).autocomplete({
        serviceUrl: '/get-similar/getSimilarCatalog?modelType=' + modelType,
        type: 'POST',
        dataType: 'json',
        paramName: 'filter',
        noCache: false,
        minChars: 2,
        onSearchStart: function (query) {
            //ajax请求前执行
            $('input[name="' + catalogId + '"]').val(-9999);
        },
        transformResult: function (response) {
            return {
                suggestions: $.map(response, function (value, key) {
                    return {value: value, data: key};
                })
            };
        },
        onSelect: function (suggestion) {
            //选中值后数据处理
            $('#' + catalogId).val(suggestion.data);
        }
    });
}

//***获取用户手机***
function AutoGetUserMobile(userId, userMobile) {
    $.ajax({
        url: "/get-similar/getUserMobile",
        type: "POST",
        data: {userId: userId},
        dataType: 'json',
        async: false,
        error: function (request) {
            //error
        },
        success: function (data) {
            $("input[name='" + userMobile + "']").val(data.mobile);
        }
    });
}